package it.niedermann.android.markdown

import androidx.annotation.ColorInt
import androidx.lifecycle.LiveData
import timber.log.Timber
import java.util.function.Consumer
import java.util.function.Function

/**
 * Can be used for editors and viewers as well.
 * Viewer can support basic edit features, like toggling checkboxes
 */
interface MarkdownEditor {
    /**
     * The given [String] will be parsed and rendered
     */
    fun setMarkdownString(text: CharSequence)

    /**
     * The given [String] will be parsed and rendered and the {@param afterRender} will be called after the rendering finished
     */
    fun setMarkdownString(text: CharSequence, afterRender: Runnable?)

    /**
     * Will replace all `@mention`s of Nextcloud users with the avatar and given display name.
     *
     * @param mentions [Map] of mentions, where the key is the user id and the value is the display name
     */
    fun setMarkdownStringAndHighlightMentions(
        text: CharSequence,
        mentions: Map<String, String>
    ) {
        setMarkdownString(text)
    }

    /**
     * @return the source [CharSequence] of the currently rendered markdown
     */
    fun getMarkdownString(): LiveData<CharSequence>

    /**
     * Similar to [.getMarkdownString] but without [LiveData]. Will remove previously set [Consumer]s.
     *
     * @param listener a [Consumer] which will receive the changed markdown string.
     */
    fun setMarkdownStringChangedListener(listener: Consumer<CharSequence>?)

    fun setEnabled(enabled: Boolean)

    /**
     * @param color which will be used for highlighting. See [.setSearchText]
     */
    fun setSearchColor(@ColorInt color: Int) {
        Timber.w("This feature is not supported by the currently used implementation.")
    }

    /**
     * See [.setSearchText]
     */
    fun setSearchText(searchText: CharSequence) {
        setSearchText(searchText, null)
    }

    /**
     * Highlights the given {@param searchText} in the [MarkdownEditor].
     *
     * @param searchText the term to highlight
     * @param current    highlights the occurrence of the {@param searchText} at this position special
     */
    fun setSearchText(searchText: CharSequence, current: Int?) {
        Timber.w("This feature is not supported by the currently used implementation.")
    }

    /**
     * Intercepts each click on a clickable element like [URLSpan]s
     *
     * @param callback Will be called on a click. When the {@param callback} returns `true`, the click will not be propagated further.
     */
    fun registerOnLinkClickCallback(callback: Function<String, Boolean>) {
        Timber.w("This feature is not supported by the currently used implementation.")
    }

    companion object {
        val TAG = MarkdownEditor::class.java.simpleName
    }
}